home *** CD-ROM | disk | FTP | other *** search
/ Visual Cafe 3 / Visual Cafe 3.ISO / Vcafe / Sample.bin / Click1.java < prev    next >
Text File  |  1998-09-15  |  3KB  |  95 lines

  1.  
  2. import java.awt.*;
  3. import java.awt.event.*;
  4. import java.applet.*;
  5.  
  6.  
  7. /** 
  8.  * This version of the Click applet extends Click1 by adding a row
  9.  * of "targets" along the top of the applet panel that highlight
  10.  * when the mouse passes over them.  The listener that handles
  11.  * enter/exit events is an instance of the private static class 
  12.  * TargetListener.   A static class can't refer to fields or variables 
  13.  * in enclosing scopes like an ordinary nested class (TargetListener
  14.  * doesn't need to - it's self contained) however it's name is
  15.  * scoped to the outer class.  For example, if TargetClass was 
  16.  * public it could be used as Click1.TargetListener.  We've made
  17.  * it private because it's not intended to be used outside of the
  18.  * implementation of Click1.
  19.  * 
  20.  * Click1 has a bug: the puck no longer tracks the mouse correctly.
  21.  * The fix is in Click2. 
  22.  * 
  23.  * This applet runs correctly in HotJava, it requires JDK 1.1.
  24.  */
  25.  
  26. public class Click1 extends Applet
  27. {
  28.   Color puckColor = new Color(200, 0, 10);
  29.   Box puck = new Box(puckColor);
  30.   ColumnOfBoxes[] targets = new ColumnOfBoxes[8];
  31.  
  32.   private final static class TargetListener extends MouseAdapter
  33.   {
  34.     private Color newBackground;
  35.     private Color oldBackground;
  36.  
  37.     TargetListener(Color newBackground) {
  38.       this.newBackground = newBackground;
  39.     }
  40.  
  41.     public void mouseEntered(MouseEvent e) {
  42.       oldBackground = e.getComponent().getBackground();
  43.       e.getComponent().setBackground(newBackground);
  44.     }
  45.  
  46.     public void mouseExited(MouseEvent e) {
  47.       e.getComponent().setBackground(oldBackground);
  48.     }
  49.   }
  50.  
  51.   public Click1()
  52.   {
  53.     MouseMotionListener movePuck = new MouseMotionAdapter() {
  54.       public void mouseMoved(MouseEvent e)
  55.       {
  56.     int x = e.getX();
  57.     int y = getSize().height - puck.getSize().height;
  58.     puck.setLocation(x, y);
  59.       }
  60.     };
  61.  
  62.     /* Create a row of targets, i.e. columns of boxes, along
  63.      * the top of the applet.  Each target column contains
  64.      * between one and four boxes.
  65.      */
  66.  
  67.     for(int i = 0; i < targets.length; i++) {
  68.       int nBoxes = 1 + (int)(Math.random() * 3.0);
  69.       float boxHue = (float)i / (float)targets.length;
  70.       Color boxColor = Color.getHSBColor(boxHue, 0.5f, 0.85f);
  71.       MouseListener targetListener = new TargetListener(boxColor.brighter());
  72.       targets[i] = new ColumnOfBoxes(boxColor, nBoxes);
  73.       targets[i].addMouseListener(targetListener);
  74.       add(targets[i]);
  75.     }
  76.  
  77.     add(puck);
  78.     addMouseMotionListener(movePuck);
  79.   }
  80.  
  81.   public static void main(String[] args)
  82.   {
  83.     WindowListener l = new WindowAdapter()
  84.       {
  85.     public void windowClosing(WindowEvent e) {System.exit(0);}
  86.       };
  87.  
  88.     Frame f = new Frame("Click");
  89.     f.addWindowListener(l); 
  90.     f.add(new Click1());
  91.     f.setSize(600, 400);
  92.     f.show();
  93.   }
  94. }
  95.